package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.entity.UserPractice;
import com.example.computer_exam_training_sys.vo.UserPracticeCourseVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserPracticeMapper {

    @Select("SELECT * FROM user_practices")
    List<UserPractice> selectAll();

    @Select("SELECT * FROM user_practices WHERE user_id = #{userId}")
    List<UserPractice> selectByUserId(Integer userId);

    @Select("SELECT * FROM user_practices WHERE user_id = #{userId} AND point_id = #{pointId}")
    List<UserPractice> selectByUserAndPoint(@Param("userId") int userId, @Param("pointId") int pointId);

    @Insert("INSERT INTO user_practices (user_id, course_id, point_id, question_count, correct_count, start_time, duration) " +
            "VALUES (#{userId}, #{courseId}, #{pointId}, #{questionCount}, #{correctCount}, now(), #{duration})")
    @Options(useGeneratedKeys = true, keyProperty = "practiceId")
    int insert(UserPractice practice);

    // 新增：统计用户某知识点练习次数
    @Select("SELECT COUNT(*) FROM user_practices WHERE user_id = #{userId} AND point_id = #{pointId}")
    int countPracticeByUserAndPoint(@Param("userId") Integer userId, @Param("pointId") Integer pointId);

    // 新增：查询用户某知识点下所有practice_id列表
    @Select("SELECT practice_id FROM user_practices WHERE user_id = #{userId} AND point_id = #{pointId}")
    List<Integer> selectPracticeIdsByUserAndPoint(@Param("userId") Integer userId, @Param("pointId") Integer pointId);

    // 你之前用 XML 做的以下两个方法，就不需要加
    List<UserPracticeCourseVO> selectPracticeCourseByUserId(Integer userId);
    List<UserPracticeCourseVO> selectAllPracticeCourse();

    @Select("SELECT COUNT(DISTINCT point_id) FROM user_practices WHERE user_id = #{userId} AND course_id = #{courseId} AND point_id IS NOT NULL")
    int countDistinctPoints(@Param("userId") Integer userId, @Param("courseId") Integer courseId);

}
